package com.egame.manage.biz.server.pu;

import java.rmi.RemoteException;
import cn.egame.common.data.BaseDao;
import cn.egame.common.data.SqlUtils;
import cn.egame.common.exception.ExceptionCommonBase;
import cn.egame.common.util.Utils;
import cn.egame.interfaces.pu.AppParameter;
import com.egame.manage.biz.server.ManageClient;
public class ParameterServiceDao extends BaseDao {

	public ParameterServiceDao() throws ExceptionCommonBase {
		super("egame_game");
	}
		
	public int toUp(int apptype,int parameterId)throws ExceptionCommonBase{
		String sql = "select sort from t_parameter_app where type = " + apptype + " and id = " + parameterId;
		int parameterSort = this.getInt(sql);
		System.out.println(sql);
		sql = "select MAX(sort) from t_parameter_app where type = " + apptype;
		int maxSort = this.getInt(sql);
		System.out.println(sql);
		sql = "update t_parameter_app set sort = sort -1 where type = " + apptype + " and sort >" + parameterSort;
		this.executeUpdate(sql);
		System.out.println(sql);
		sql = "update t_parameter_app set sort = " + maxSort + " where type = " + apptype + " and id = " + parameterId;
		System.out.println(sql);
		return this.executeUpdate(sql);
		
	}
	
	public int toDown(int apptype,int parameterId)throws ExceptionCommonBase{
		String sql = "select sort from t_parameter_app where type = " + apptype + " and id = " + parameterId;
		int parameterSort = this.getInt(sql);
		System.out.println(sql);
		sql = "select MIN(sort) from t_parameter_app where type = " + apptype;
		int minSort = this.getInt(sql);
		System.out.println(sql);
		sql = "update t_parameter_app set sort = sort+1 where type = " + apptype + " and sort <" + parameterSort;
		this.executeUpdate(sql);
		System.out.println(sql);
		sql = "update t_parameter_app set sort = " + minSort + " where type = " + apptype + " and id = " + parameterId;
		System.out.println(sql);
		return this.executeUpdate(sql);
	}
	
	public int deleteKeyword(int id)throws ExceptionCommonBase{
		String sql = "update t_parameter_app set enable = 0 where id = " + id;
		return  this.executeUpdate(sql);
	}
	
	public int deleteKeywords(int type)throws ExceptionCommonBase{
		String sql = "update t_parameter_app set enable = 0 where type = " + type;
		return  this.executeUpdate(sql);
	}

	public int upper(int type,int parameterId) throws ExceptionCommonBase {		
		String sql = "select sort from t_parameter_app where type = " + type + " and id =" + parameterId;
		int sort = this.getInt(sql);
		sql = "SELECT min(sort) from t_parameter_app WHERE type = " + type + " and sort >" + sort;
		int upSort = this.getInt(sql);	
		if(upSort>sort){
			sql = "update t_parameter_app set sort = " + sort +" where type = " + type + " and sort = " + upSort;
			this.executeUpdate(sql);
			sql = "update t_parameter_app set sort = " + upSort + " where type = " + type + " and id =" + parameterId;
			return this.executeUpdate(sql);
		}
		return -1;
	}

	public int lower(int type,int parameterId) throws ExceptionCommonBase {
		String sql = "select sort from t_parameter_app where type = " + type + " and id =" + parameterId;
		int sort = this.getInt(sql);
		sql = "SELECT max(sort) from t_parameter_app WHERE type = " + type + " and sort <" + sort;
		int lowerSort = this.getInt(sql);		
		if(lowerSort>0){
			sql = "update t_parameter_app set sort = " + sort +" where type = " + type + " and sort = " + lowerSort;
			this.executeUpdate(sql);
			sql = "update t_parameter_app set sort = " + lowerSort + " where type = " + type + " and id =" + parameterId;
			return this.executeUpdate(sql);
		}
		return -1;
	}

	public int editKeyword(int type,int parameterId,String name) throws ExceptionCommonBase {
		if(name != null){
			String sql = "update t_parameter_app set name = " + SqlUtils.QuataStr(name) + " where type = " + type + " and id =" + parameterId;
			return this.executeUpdate(sql);
		}
		return -1;
	}

	public String getParameterNameById(int pId)throws ExceptionCommonBase{
		String sql = "select name from t_parameter_app where id = " + pId;
		String result = this.getString(sql);
		return (result==null?"":result);
	}
	
	public String getParameterNameById(int appId, long loginUserId, int pId,String remark)throws RemoteException{
		AppParameter bean = this.getAppParameter(appId, loginUserId, pId,remark);
		if(bean != null){
			return bean.getName()==null?"":bean.getName();
		}
		return "";
	}
	
	public AppParameter getAppParameter(int appId, long loginUserId, int pId,String remark)throws RemoteException{
		if(!Utils.stringIsNullOrEmpty(remark)){
			String sql = "select id from t_parameter_app where parent_id = " + pId + " and remark = " + SqlUtils.QuataStr(remark);
			System.out.println(sql);
			return ManageClient.getInstance().getAppParameterById(appId, loginUserId, this.getInt(sql));			
		}
		return null;
	}
	
	public boolean hasExist(int parameterId,String name)throws ExceptionCommonBase{
		if(!Utils.stringIsNullOrEmpty(name)){
			String sql = "select count(*) from t_parameter_app where parent_id = " + parameterId + " and name = " + SqlUtils.QuataStr(name.trim());
			System.out.println(sql);
			return this.getInt(sql)>0;
		}
		return false;
	}

	public int getMaxSortNo(int parameterId)throws RemoteException{
		String sql = "select MAX(sort) from t_parameter_app where parent_id = " + parameterId;
		System.out.println(sql);
		return this.getInt(sql);
	}	
	
	public int getIdByNameAndParentId(String name,String remark,int parentId) throws ExceptionCommonBase{
		String sql = "select id from t_parameter_app where parent_id = " + parentId + "  and name = " + SqlUtils.QuataStr(name.trim()) + "  and remark = " + SqlUtils.QuataStr(remark.trim());
		return this.getInt(sql);
	}
}
